﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net;
using System.IO;

public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                //// Track Visitors
                //string ipAddress = IpAddress();
                //string hostName = Dns.GetHostByAddress(ipAddress).HostName;
                //StreamWriter wrtr = new StreamWriter(Server.MapPath("visitors.log"), true);
                //wrtr.WriteLine(DateTime.Now.ToString() + " | " + ipAddress + " | " + hostName + " | " + Request.Url.ToString());
                //wrtr.Close();

                // HitCounter
                string PageUrlProductDetail = "/Page/ProductDetail/Default.aspx";
                string PageUrl = Request.ServerVariables["SCRIPT_NAME"].ToString();
                if (PageUrl == PageUrlProductDetail)
                {
                    PageUrl = PageUrl + "?ProductID=" +  webapp4U.Globals.GetIntFromQueryString("ProductID");
                }

                if (Request.Cookies[PageUrl] == null)
                {
                    HttpCookie cookie = new HttpCookie(PageUrl);
                    cookie.Expires = DateTime.Now.AddHours(12);
                    Response.Cookies.Add(cookie);

                    //Lưu lại 
                    webapp4U.BOL.HitCounter obj = new webapp4U.BOL.HitCounter();
                    obj.PageUrl = PageUrl;
                    webapp4U.BOL.HitCounter.Save(obj);
                }
            }
            catch (Exception ex) { }
        }

    }

    private string IpAddress()
    {

        string strIpAddress;

        strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (strIpAddress == null)

            strIpAddress = Request.ServerVariables["REMOTE_ADDR"];

        return strIpAddress;

    }
}
